package com.fristvue.controller;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fristvue.po.Users;
import com.fristvue.po.Util.Page;
import com.fristvue.service.UsersService;
import com.fristvue.utils.JwtUtil;
import com.fristvue.utils.RandomCode;



@RestController
public class UsersController {
	/**
	 * 自动注入业务层
	 */
	@Autowired
	private UsersService usersService;
	/**
	 * 自动注入工具类
	 */
	@Autowired
	private RandomCode randomCode;
	
	/**
	 * 前端传值后端查出指定的值,然后返回前端
	 * @param userId
	 * @return
	 */
	@RequestMapping("findById")
	public Users findByIdUsers(Integer userId) {
		return this.usersService.selectByPrimaryKey(userId);
	}
	/**
	 * 修改用户的数据
	 * @param users
	 * @return
	 */
	
	@PutMapping("eidtUserFindById")
	public boolean editUser(@RequestBody Users users) {
		return this.usersService.editUsers(users);
	}
	
	/**
	 *添加用户
	 */
	@PutMapping("insertUser")
	public boolean insertUseers(@RequestBody Users users) {
		try {
			Date datef= new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2021-05-21").getTime());
			users.setSignInData(datef);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.usersService.insertUser(users);
	}
	
	/**
	 * 查询出所有值,并将这些全传回前端
	 * @return
	 */
	@RequestMapping("findUserAll/{page}/{size}")
	public Page<Users> findAllUsers(@PathVariable("page") Integer page,
			@PathVariable("size") Integer size, @RequestBody Users user, String userName){
		Integer num = page * size;
		return this.usersService.selectAllUsers(num, size, user);
	}
	
	/**
	 * 删除用户
	 * @param userId
	 * @return
	 */
	@DeleteMapping("deleteUserById")
	public boolean deleteUsers(Integer userId) {
		return this.usersService.deleteByPrimaryKey(userId);	
	}
	/**
	 * 使用手机号码和密码进行登录
	 * @param users
	 * @return
	 */
	@GetMapping("UsePhoneLogin")
	 public Users PhoneLogin( Users users) {
		Users getUser = this.usersService.
				UsePhoneLoginFindUser(users.getPhone(),users.getPassword());
		if(getUser != null) {
			getUser.setToken(JwtUtil.createToken(getUser));
			return getUser;
		}
		return null;
	}
	
	/**
     * 前端请求发送邮箱
     */
    @GetMapping("sendMail")
    public String SendMailCode(String mail){
        return String.valueOf(this.randomCode.verifyCode());
    }

	
	/**
	 * 使用邮箱进行辅助登录
	 * @param mail
	 * @return
	 */
	@GetMapping("UseEmailLogin")
    public Users EmailLogin(String mail) {
		return this.usersService.UseEmailLoginFindUser(mail);
	}
	
	/**
	 * 验证Token,用户是否合法
	 * @return
	 */
	@GetMapping("verifyToke")
	public Boolean checkToken(HttpServletRequest request) {
		String token = request.getHeader("token");
		return JwtUtil.checkToken(token);
	}
	/**
	 * 传值给前端,但只传id和用户名
	 */
	@GetMapping("whereUser")
	public List<Users> whereListUser(){
		return this.usersService.WhereUsers();
	}
}
